package com.lh.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.lh.pojo.Dept;
import com.lh.pojo.User;
import com.lh.service.DeptService;
import com.lh.service.UserService;

/**
 * @Description:现在的你决定了未来的你
 * @Company: www.lihong.com
 * @ClassName:UserController
 * @author 李宏
 * @date 2018年1月30日下午2:23:12
 * @version 3.0
 */
@Controller
public class UserController {
	@Resource
	private UserService us;
	@Resource
	private DeptService ds;

	@RequestMapping("/user/find.action")
	public String find(Model model) {
		List<User> users = us.find();
		model.addAttribute("users", users);
		return "user_list";
	}

	@RequestMapping("/user/toAdd.action")
	public String toAdd(Model model) {
		List<Dept> depts = ds.find_dept_list();
		model.addAttribute("depts", depts);
		return "user_add";
	}

	@RequestMapping(value = "/user/add.action", method = { RequestMethod.POST })
	public String add(User user, @RequestParam(value = "photo") MultipartFile photo, HttpServletRequest request,
			Model model) {
		try {
			// 1.获取服务器路径(获取网站根目录)
			String path = request.getServletContext().getRealPath("/upload/user_imgs");
			// 2.判断服务器是否有此路径
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			// 3.获取文件名
			String photoName = photo.getOriginalFilename();
			// 4.处理文件名
			photoName = UUID.randomUUID() + photoName;
			// 5.指定上传文件的目录
			File toAddress = new File(path, photoName);
			// 6.开始文件长传
			FileUtils.writeByteArrayToFile(toAddress, photo.getBytes());
			// 7.文件上传成功，把路径封装到users对象中
			user.setU_img("/upload/user_imgs/" + photoName);
			// 8.保存到数据库中
			us.add(user);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/user/find.action";
	}

	@RequestMapping("/user/toUpdate.action")
	public String toUpdate(Integer id, Model model) {
		User user = us.findById(id);
		model.addAttribute("user", user);
		List<Dept> depts = ds.find_dept_list();
		model.addAttribute("depts", depts);
		return "user_update";
	}

	@RequestMapping(value = "/user/update.action", method = {})
	public String update(User user, @RequestParam(value = "photo") MultipartFile photo, HttpServletRequest request,
			Model model) {
		String m = photo.getOriginalFilename();
		if (m == "") {
			String u_img = request.getParameter("u_img");
			user.setU_img(u_img);
			us.update(user);
		} else {
			try {
				// 1.获取服务器路径(获取网站根目录)
				String path = request.getServletContext().getRealPath("/upload/user_imgs");
				// 2.判断服务器是否有此路径
				File file = new File(path);
				if (!file.exists()) {
					file.mkdirs();
				}
				// 3.获取文件名
				String photoName = photo.getOriginalFilename();
				// 4.处理文件名
				photoName = UUID.randomUUID() + photoName;
				// 5.指定上传文件的目录
				File toAddress = new File(path, photoName);
				// 6.开始文件长传
				FileUtils.writeByteArrayToFile(toAddress, photo.getBytes());
				// 7.文件上传成功，把路径封装到users对象中
				user.setU_img("/upload/user_imgs/" + photoName);
				// 8.保存到数据库中
				us.update(user);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "redirect:/user/find.action";
	}
	
	@RequestMapping("/user/remove.action")
	public String remove(Integer id){
		us.remove(id);
		return "redirect:/user/find.action";
	}
}